﻿package ua.mihaylenko.cardsplugin.components.cardeditor
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import fl.controls.RadioButton;
	import ua.mihaylenko.cardsplugin.common.FlashVars;
	import ua.mihaylenko.cardsplugin.data.*;
	public class ECEImage extends MovieClip
	{
		public var _btnClickHandler:SimpleButton;
		public var _stxName:TextField;
		public var _rbCheck:RadioButton;
		private var _aStock:Array;
		private var _oClick:Object;
		private var _selectedIndex:int = -1;		
		
		private var _colourPicker:String;//if the image field has attribute colourpicker with value (RGB)
		private var _selectedColour:String;//to remember colour picker colour for image field
		
		public function ECEImage()
		{
			_btnClickHandler.addEventListener(MouseEvent.CLICK, onClickMe);
			_stxName.mouseEnabled = false;
		}
		private function onClickMe(oEvent:MouseEvent)
		{
			_oClick.Function.apply(_oClick.Object, [this]);
		}
		
		public function set cpColor(color:String){
			_selectedColour=color;
		}
		
		public function get cpColor(){
			return _selectedColour;
		}
		
		public function set click(oClick:Object)
		{
			_oClick = oClick;
		}
		public function set selected(bSelect:Boolean)
		{
			_rbCheck.selected = bSelect;
		}
		public function get selected():Boolean
		{
			return _rbCheck.selected;
		}
		public function set stockImages(aStock:Array)
		{
			_aStock = new Array();
			for (var i:int = 0; i < aStock.length; i++)
			{
				var cData:DataThumb = new DataThumb();
				cData.thumb = FlashVars.getInstance().getData("domain") + "/photothumbs/" + aStock[i].src;
				cData.id = aStock[i].value;
				cData.link = aStock[i].name;
				cData.selected = false;
				_aStock.push(cData);
			}
			
			this.checkColorPickerAndStockImage();
		}
		public function get stockImages():Array
		{
			return _aStock;
		}
		public function get selectIndex():int
		{
			return _selectedIndex;
		}
		public function set selectIndex(value:int):void
		{
			_selectedIndex = value;
		}
		public function get selectedID():String
		{
			if (selectIndex >= 0)
			{
				return DataThumb(_aStock[this.selectIndex]).id;
			}
			
			//if the color is selected using colour picker
			if(_selectedColour){
				return _selectedColour;
			}
			
			return "";
		}
		public function set title(sTitle:String)
		{
			_stxName.text = sTitle;
		}
		public function get title():String
		{
			return _stxName.text;
		}
		
		public function set colourpicker(cPicker:String)
		{
			_colourPicker = cPicker;
			this.checkColorPickerAndStockImage();
		}
		public function get colourpicker():String
		{
			return _colourPicker;
		}
		
		// disable radiobutton when there is no color picker and stock image - by luke
		protected function checkColorPickerAndStockImage():void{
		    if((this._colourPicker == null || this._colourPicker == '' || !this._colourPicker)
			    && (this._aStock == null || this._aStock.length == 0)){
			    this._rbCheck.enabled = false;
				_btnClickHandler.removeEventListener(MouseEvent.CLICK, onClickMe);
			}else{
			    this._rbCheck.enabled = true;
			    _btnClickHandler.addEventListener(MouseEvent.CLICK, onClickMe);
			}
		}
	}
}